﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RhythmGame
{
    internal class BeatMapLoader
    {
        private static BeatMapLoader _instance;
        private string songsPath = "Songs";

        private BeatMapLoader() { }

        public static BeatMapLoader Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new BeatMapLoader();
                }
                return _instance;
            }
        }

        // 加载一个节奏图
        public BeatMap LoadBeatMap(string name, string rgFileName = "beatmap.rg")
        {
            var map = new BeatMap();

            // 用 Path.Combine 拼接路径
            string beatmapPath = Path.Combine(songsPath, name, rgFileName);

            // 输出调试信息
            //System.Windows.Forms.MessageBox.Show("实际路径: " + beatmapPath + "\n当前工作目录: " + Environment.CurrentDirectory);

            if (!File.Exists(beatmapPath))
            {
                throw new FileNotFoundException("谱面文件不存在", beatmapPath);
            }

            // 从文件加载节奏图
            string[] lines = System.IO.File.ReadAllLines(beatmapPath);
            map.InitBeatMap(
                lines[0],
                lines[1],
                lines[2],
                lines[3],
                lines[4],
                TimeSpan.Parse(lines[5]),
                int.Parse(lines[6]),
                int.Parse(lines[7]),
                lines[8],
                lines[9],
                lines[10],
                lines[11],
                name);

            // 元素信息从第14行开始（计数13）
            for (int i = 13; i < lines.Length; i++)
            {
                string[] element = lines[i].Split(',');
                int typeId = int.Parse(element[0]);
                int clickTime = int.Parse(element[1]);
                int x = int.Parse(element[2]);
                int width = int.Parse(element[3]);
                switch (typeId)
                {
                    case 1:
                        ElementTap_info element_info = new ElementTap_info();
                        element_info.TypeId = typeId;
                        element_info.ClickTime = clickTime;
                        element_info.X = x;
                        element_info.Width = width;
                        map.AddElement(element_info);
                        break;
                    case 2:
                        ElementBreakTap_info elementBreakTap_info = new ElementBreakTap_info();
                        elementBreakTap_info.TypeId = typeId;
                        elementBreakTap_info.ClickTime = clickTime;
                        elementBreakTap_info.X = x;
                        elementBreakTap_info.Width = width;
                        map.AddElement(elementBreakTap_info);
                        break;
                    case 3:
                        ElementProtectedTap_info elementProtectedTap_Info = new ElementProtectedTap_info();
                        elementProtectedTap_Info.TypeId = typeId;
                        elementProtectedTap_Info.ClickTime = clickTime;
                        elementProtectedTap_Info.X = x;
                        elementProtectedTap_Info.Width = width;
                        map.AddElement(elementProtectedTap_Info);
                        break;
                    case 4:
                        ElementProtectedBreakTap_info elementProtectedBreakTap_info = new ElementProtectedBreakTap_info();
                        elementProtectedBreakTap_info.TypeId = typeId;
                        elementProtectedBreakTap_info.ClickTime = clickTime;
                        elementProtectedBreakTap_info.X = x;
                        elementProtectedBreakTap_info.Width = width;
                        map.AddElement(elementProtectedBreakTap_info);
                        break;
                    case 5:
                        ElementDrag_info elementDrag_info = new ElementDrag_info();
                        elementDrag_info.TypeId = typeId;
                        elementDrag_info.ClickTime = clickTime;
                        elementDrag_info.X = x;
                        elementDrag_info.Width = width;
                        map.AddElement(elementDrag_info);
                        break;
                    default:
                        break;
                }

            }

            map.Finish();

            return map;
        }

        // 加载所有节奏图
        public List<BeatMap> LoadAllBeatMaps()
        {
            List<BeatMap> maps = new List<BeatMap>();
            string[] dirs = System.IO.Directory.GetDirectories(songsPath);
            foreach (string dir in dirs)
            {
                maps.Add(LoadBeatMap(System.IO.Path.GetFileName(dir)));
            }
            return maps;
        }
    }
}
